package com.bsj.media.init;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsj.media.tool.HttpPlayBackTool;
import com.bsj.media.tool.HttpPlayVideoTool;
import com.bsj.power.common.def.protocol.rtp.RTPInfo;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 管道缓存所有的播放链路
 */
@Slf4j
public class SendChannelUntil {

    private static final Map<String, SendCachedData> cachedDataMap = new ConcurrentHashMap<>();

    public static SendCachedData gainObj(String simChannel) {
        return cachedDataMap.get(simChannel);
    }

//    public static void addHttp(String simChannel, ChannelHandlerContext channelHandlerContext) {
//        SendCachedData sendCachedData = cachedDataMap.get(simChannel);
//        if (sendCachedData == null) {
//            sendCachedData = new SendCachedData(simChannel);
//        }
//        sendCachedData.pushDataHttp(channelHandlerContext);
//        cachedDataMap.put(simChannel, sendCachedData);
//    }

    public static void addHttpLiveVideo(String simChannel, ChannelHandlerContext channelHandlerContext,
                                        String mediaType) {
        SendCachedData sendCachedData = cachedDataMap.get(simChannel);
        if (sendCachedData == null) {
            sendCachedData = new SendCachedData(simChannel);
        }
        sendCachedData.pushDataHttpLiveVideo(mediaType, channelHandlerContext);
        cachedDataMap.put(simChannel, sendCachedData);
    }

    public static void addHttpDownVideo(String simChannel,
                                        ChannelHandlerContext channelHandlerContext,
                                        String fileName, String mediaType) {
        SendCachedData sendCachedData = cachedDataMap.get(simChannel);
        if (sendCachedData == null) {
            sendCachedData = new SendCachedData(simChannel);
        }
        sendCachedData.pushDataDownVideo(mediaType, channelHandlerContext, fileName);
        cachedDataMap.put(simChannel, sendCachedData);
    }

    public static void pushRTPData(RTPInfo rtpInfo, Integer port) {
        int channelNo = rtpInfo.getChannelNo();
        String sim = rtpInfo.getSim();
        String key = sim + "-" + channelNo;
        if (port != null) {
            key = key + "-" + port;
        }
        int dataType = rtpInfo.getDataType();
        int pageMark = rtpInfo.getPageMark();
        SendCachedData sendCachedData = cachedDataMap.get(key);
        if (sendCachedData != null) {
            sendCachedData.handleData(rtpInfo);
        }
    }

//    public static void addNature(String simChannel, ChannelHandlerContext channelHandlerContext) {
//        SendCachedData sendCachedData = cachedDataMap.get(simChannel);
//        if (sendCachedData == null) {
//            sendCachedData = new SendCachedData(simChannel);
//        }
//        sendCachedData.pushDataNature(channelHandlerContext);
//        cachedDataMap.put(simChannel, sendCachedData);
//    }

    public static int closeMediaFlag(String simChannel) {
        SendCachedData sendCachedData = cachedDataMap.get(simChannel);
        if (sendCachedData == null) {
            return 0;
        }
//        log.info("cachedDataMap : {}", JSON.toJSONString(cachedDataMap.size()));
//        log.info("closeMediaFlag : {}", JSON.toJSONString(sendCachedData.saveDto()));
        return sendCachedData.saveDto();
    }

    public static void del(String simChannel, ChannelHandlerContext channelHandlerContext) {
        SendCachedData sendCachedData = cachedDataMap.get(simChannel);
        if (sendCachedData != null) {
            sendCachedData.del(channelHandlerContext);
            String[] split = simChannel.split("-");
            if (split.length == 3) {
                //如果是回放则需要关闭UDP服务端,并且直接发送回放关闭消息给SIP服务端
                Integer port = Integer.parseInt(split[2]);
                //回放的通道号本系统默认设置加126
                Integer ch = Integer.parseInt(split[1]) - 126;
                HttpPlayBackTool.closePlayBack(port, split[0], ch);
                return;
            }
            //没有链路了,代表已经无人观看
            if (sendCachedData.clearFlag()) {
                cachedDataMap.remove(simChannel);
                //发送关闭直播指令
                HttpPlayVideoTool.liveCloseCmd(split[0], Integer.parseInt(split[1]), true);
                //直接删除解码器中的数据
//                Gb28181RtpCachedThread.del(simChannel);
//                CloseThread.pushChannel(simChannel);
            }
        }
    }

    public static void del(String simChannel) {
        SendCachedData remove = cachedDataMap.remove(simChannel);
        log.info("关闭指令删除通道: {}", simChannel);
        if (remove != null) {
            remove.close();
        }
    }
}
